package com.example.capsule_chat0322.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.capsule_chat0322.R;
import com.example.capsule_chat0322.bean.FriendApplyBean;
import com.example.capsule_chat0322.bean.FunctionBean;
import com.example.capsule_chat0322.interfaces.FriendFunctionItemOnClickInterface;

import java.util.List;

/**
 * @Author zhuzhiming
 * @Date 2022/5/25 21:21
 */
public class FriendMoreFunctionAdapter extends RecyclerView.Adapter<FriendMoreFunctionAdapter.ViewHold> implements View.OnClickListener {

    public List<FunctionBean> functionBeanList;
    private FriendFunctionItemOnClickInterface friendFunctionItemOnClickInterface;
    private RecyclerView rvParent;

    public void setFunctionItemOnClickInterface(FriendFunctionItemOnClickInterface friendFunctionItemOnClickInterface) {
        this.friendFunctionItemOnClickInterface = friendFunctionItemOnClickInterface;
    }

    static class ViewHold extends RecyclerView.ViewHolder {

        LinearLayout functionItem;
        TextView functionName;
        TextView message_count;

        public ViewHold(@NonNull View itemView) {
            super(itemView);
            functionItem = itemView.findViewById(R.id.friend_more_function_item);
            functionName = itemView.findViewById(R.id.friend_more_function_name);
            message_count = itemView.findViewById(R.id.message_count);
        }
    }


    public FriendMoreFunctionAdapter(List<FunctionBean> functionBeanList) {
        this.functionBeanList = functionBeanList;
    }

    @NonNull
    @Override
    public ViewHold onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        rvParent = (RecyclerView) parent;
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.friend_more_function_item, parent, false);
        view.setOnClickListener(this);
        return new FriendMoreFunctionAdapter.ViewHold(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHold holder, int position) {

        FunctionBean functionBean = functionBeanList.get(position);
        holder.functionItem.setVisibility(View.VISIBLE);
        holder.functionName.setText(functionBean.getFunctionName());

        if (!functionBean.getFunctionName().equals("好友申请")) {
            holder.message_count.setVisibility(View.GONE);
        } else {
            List<FriendApplyBean> applyList = functionBean.getApplyList();
            if (applyList.size() == 0 || functionBean.getApplyListStatus() != null) {
                holder.message_count.setVisibility(View.GONE);
            } else {
                holder.message_count.setVisibility(View.VISIBLE);

                int count = 0;
                for (FriendApplyBean friendApplyBean : applyList) {
                    if (friendApplyBean.getApplyStatus() == null) {
                        count++;
                    }
                }
                holder.message_count.setText(String.valueOf(count));
            }
        }

    }

    @Override
    public int getItemCount() {
        return functionBeanList.size();
    }


    @Override
    public void onClick(View view) {
        //根据RecyclerView获得当前View的位置
        int position = rvParent.getChildAdapterPosition(view);
        //程序执行到此，会去执行具体实现的onItemClick()方法
        if (friendFunctionItemOnClickInterface != null) {
            friendFunctionItemOnClickInterface.onItemClick(rvParent, view, position, functionBeanList.get(position));
        }
    }


}
